package ltd.followkba.comment;

import java.io.File;
import java.util.List;

import ltd.advskin.MRAM;
import ltd.advskin.base.KcTwoListener;
import ltd.advskin.utils.SysUtils;
import ltd.followkba.comment.bean.BOldRankInfo;
import ltd.followkba.comment.bean.BTaskRun;
import ltd.followkba.comment.utils.CMoreUtils;
import ltd.followkba.comment.utils.CMultiThread;
import ltd.followkba.comment.utils.DbsTypeUserID;

import static ltd.followkba.comment.MKCC.LOGIN_TASK_RUN;
import static ltd.followkba.comment.MKCC.cKCB_SW_CHANGBA;
import static ltd.followkba.comment.MKCC.cKCB_SW_HAOCHANG;
import static ltd.followkba.comment.MKCC.cKCB_SW_MAXIMAL;
import static ltd.followkba.comment.utils.DbsTypeUserID.USER_DETAIL;
import static main.MAPI.MSLEEP;
import static ltd.advskin.MSKIN.MSTRING;


/**
 * Created by Administrator on 2018/4/8.
 */

public class MKCS {
    public static byte gDayOfMonth;
    public static boolean isSystemClose;
    public static byte gTaskUpdate;
    public static List<BOldRankInfo>[] mBRankInfo;
    public static List<BTaskRun>[] mBTaskRuns;
    public static String gsTaskPath[];
    public static String gsWorkStatus[];
    public static String gsRankName[];
    public static int[] gRankIndex;

    public static String gsTempPath;
    public static String gsPhotoPath;
    public static String gsCounterMemory;
    public static int g4SucessCounter;
    public static int g4ActiveTime = 0;
    public static int g4SetMaxTask;

    public static String gsNormalComment;
    public static String gsNewComment;
    public static String gsOldComment;
    public static int g4NewCommTime;
    public static int g4OldCommTime;
    public static String gsUploadSongIntro;
    public static int g4UploadSongIntro;
    public static boolean isStopComment;
    public static byte gAutoReadCommTimer;
    public static byte gAutoReadCommSet;
    public static boolean FSinaWeiboTx;
    public static byte gSinaWeiboRx;
    public static String gsChangBaSsid;
    private static RegisteChangBa mRegisterChangBa;
    private static int g4RegisterChangBa;

    public static CMultiThread[] mCMultiThread;
    public static byte gAssignTask;
 //   public static byte[] mAppList = {cKCB_SW_KBA, cKCB_SW_CHANGBA, cKCB_SW_KUWOKG, cKCB_SW_HAOCHANG};

    public static void startRun(){
        mBRankInfo = new List[cKCB_SW_MAXIMAL];
        mBTaskRuns = new List[cKCB_SW_MAXIMAL];
        gsTaskPath = new String[cKCB_SW_MAXIMAL];
        gsRankName = new String[cKCB_SW_MAXIMAL];
        gsWorkStatus = new String[cKCB_SW_MAXIMAL];
        gRankIndex = new int[cKCB_SW_MAXIMAL];
        String gsLocal_1;
        int g4Local_1 = 0;
        do {
            gsTaskPath[g4Local_1] =  String.format("%s%s/", MRAM.gsFilePath, MUTIL.GET_APP_TYPE_EN_NAME(g4Local_1));
//            gsTaskFile[g4Local_1] = String.format("%s%s.ini", MRAM.gsFilePath, MUTIL.GET_APP_TYPE_EN_NAME(g4Local_1));
//MAPI.DELETE_FILE(gsTaskFile[g4Local_1]);
        } while (++g4Local_1 < cKCB_SW_MAXIMAL);
//MBAPP.MSTRING("START_RUN 1 "+MAPI.GET_FILE_LENGTH(gsTaskFile[1]));
        gsCounterMemory = MRAM.gsFilePath + "counter.dbs";
        gsTempPath = MRAM.gsFilePath + "Temp/";
        final File file = new File(gsTempPath);
        if (!file.exists()) {
            file.mkdirs();
        }
        gsPhotoPath = MRAM.gsCachePath + "photo/";
        File file1 = new File(gsPhotoPath);
        if (!file1.exists()) {
            file1.mkdirs();
        }
        SysUtils sysUtils = new SysUtils();
        gsNormalComment = sysUtils.readTextFile(MRAM.gsFilePath + "NormalComm.txt");
        gsNewComment = sysUtils.readTextFile(MRAM.gsFilePath + "NewComment.txt");
        gsOldComment = sysUtils.readTextFile(MRAM.gsFilePath + "OldComment.txt");
        gsUploadSongIntro = sysUtils.readTextFile(MRAM.gsFilePath + "UploadSongIntro.txt");
        g4NewCommTime = MBAPP.RD_PRIVATE_VALUE("NewSongTime", MBAPP.gsMainMem);
        g4OldCommTime = MBAPP.RD_PRIVATE_VALUE("OldSongTime", MBAPP.gsMainMem);
        g4UploadSongIntro = MBAPP.RD_PRIVATE_VALUE("SongIntro", MBAPP.gsMainMem);

        MBAPP.gLoginStatus = LOGIN_TASK_RUN;
        gAssignTask = 1;
        DbsTypeUserID mainUserID = new DbsTypeUserID(USER_DETAIL, 9);
        g4SucessCounter = mainUserID.getMemoryInt("g4SucessCounter");
        mCMultiThread = new CMultiThread[cKCB_SW_MAXIMAL];
        for (int counter = 0; counter < cKCB_SW_MAXIMAL; counter++){
            mCMultiThread[counter] = new CMultiThread();
            mCMultiThread[counter].setListener(counter, new KcTwoListener() {
                @Override
                public void onMessage(Object object1, Object object2){
//                    MSTRING("MKCS onMessage A "+type+" "+object+" "+object);
//                    CWriteComment cWriteComment = new CWriteComment();
//                    cWriteComment.startTask((BTaskRun) object2);
                }
            });
        }
        new Thread(new Runnable() {
            @Override
            public void run () {
                while (!MKCS.isSystemClose) {
                    MSLEEP(1000);
//                    if (mMyUtils.checkIsLogin()) {
//                        MKCS.onTimer();
//                    }
                }
            }
        }).start();
    }
    public static void assignTask(){
        MKCS.gAssignTask = 1;
    }
    public static void onTimer(){
//        MSTRING("MKCS onTimer AA "+MKCS.gAssignTask);
        ++MKCS.g4ActiveTime;
        if (MKCS.gAssignTask > 0){
            CMoreUtils cMoreUtils = new CMoreUtils();
            cMoreUtils.assignAllTask();
            return;
        }
        if (MKCS.gAssignTask == 0 && mBTaskRuns != null) {
            BTaskRun bTaskRun;
            for (int appType = 0; appType < cKCB_SW_MAXIMAL; appType++) {
                int length = mBTaskRuns[appType].size();
                for (int counter = 0; counter < length; counter++) {
                    bTaskRun = mBTaskRuns[appType].get(counter);
//                if (true && bTaskRun.AppType == cKCB_SW_CHANGBA) {
//                    continue;
//                }
                    ++bTaskRun.StepCounter;
                    if (!bTaskRun.mBAccount.IsPause && !bTaskRun.IsBusy && bTaskRun.StepCounter >= bTaskRun.mBAccount.StepTime) {
                        bTaskRun.IsBusy = true;
                        MKCS.mCMultiThread[bTaskRun.AppType].setTask(bTaskRun);
                    }
                }
            }
        }
//        MSTRING("MKCS onTimer BB "+MKCS.gAssignTask);
    }


    public static KgAppBase SwitchBaseApp(int appType){
        switch (appType){
            case cKCB_SW_CHANGBA:
                return new KgChangBa();
            case cKCB_SW_HAOCHANG:
                return new KgHaoChang();
            case 0x100|cKCB_SW_CHANGBA:
                return new TransChangBa();
            case 0x100|cKCB_SW_HAOCHANG:
                return new TransHaoChang();
        }
        return new KgVvMusic();
    }
    public static KgAppBase CloneNewBaseApp(int appType){
        MSTRING("CloneNewBaseApp "+appType+" "+ mBTaskRuns);
        if (mBTaskRuns != null) {
            int g4Local_1 = 0;
            while (g4Local_1 < MKCS.mBTaskRuns[appType].size()){
                if (mBTaskRuns[appType].get(g4Local_1).AppType == appType) {
                    if ("ok".equals(mBTaskRuns[appType].get(g4Local_1).mBAccount.AccountStatus)) {
                        KgAppBase kgAppBase = SwitchBaseApp(appType);
//                        mBTaskRuns[appType].get(g4Local_1).OwnApp.cloneAccountInfo(kgAppBase);
                        return kgAppBase;
                    }
                }
                ++g4Local_1;
            }
        }
        return null;
    }
    public static String GetBingSongIntro(boolean readOnly){
//MSTRING("gsUploadSongIntro 1 "+g4UploadSongIntro+" "+gsUploadSongIntro);
        if (!"".equals(gsUploadSongIntro)){
            String[] params = gsUploadSongIntro.split("\n");
            if (params.length > 0){
                String gsLocal_1 = params[g4UploadSongIntro % params.length];
                gsLocal_1 = gsLocal_1.replace("\r", "");
//MSTRING("gsUploadSongIntro 2 "+gsLocal_1);
                if (readOnly){
                    ++g4UploadSongIntro;
                    MBAPP.WR_PRIVATE_VALUE("SongIntro", g4UploadSongIntro, MBAPP.gsMainMem);
                }
                return gsLocal_1;
            }
        }
        return "";
    }


    public static void systemClose() {                    // 退出应用
        isSystemClose = true;
    }
//    public static void ShowToAllScreen(String message){
//        int g4Local_1 = 0;
//        do {
//            gsWorkStatus[g4Local_1] = message;
//        } while (++g4Local_1 < cKCB_SW_MAXIMAL);
//    }

}


